/*
    Project: OCMF (Operant Chamber Management Framework)
    Version: 1.0
    Author: Copyright (C) 2010, Samuel Delaney & Andrew Harvey
    Licence:
        This file is part of OCMF.

            OCMF is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.

            OCMF is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.

            You should have received a copy of the GNU General Public License
            along with OCMF.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Buzzer.h"

//define static variable
unsigned char Buzzer::pin = 0;

Buzzer::Buzzer(QDomElement buzzerElement, Action * parentAction) : PortAction(parentAction)
{
    //get and set the duration from the xml node
    duration = buzzerElement.attributeNode("Duration").value().toUInt(NULL,10);

    //get the boolean string from the xml node
    QString pulseString = buzzerElement.attributeNode("Pulse").value();

    //set the pulse value from the pulse string
    pulse = (pulseString == "True" ? true : false);
}

void Buzzer::Do()
{
    //log result
    Action::WriteResult(ToResultXml());

    //do the parallel port stuff
    if(!pulse)
        Port->TogglePort(pin, duration);
    else
    {
        int count = 0;
        while(count < duration)
        {
            Port->TogglePort(pin, 1);
            count += 2;
        }
    }
}

QDomElement Buzzer::ToResultXml()
{
    QDomElement buzzerXmlElement;

    //set tag name
    buzzerXmlElement.setTagName("Buzzer");

    //set attributes
    buzzerXmlElement.setAttribute("TimeStamp", TimeStamp.toString(Qt::ISODate));

    buzzerXmlElement.setAttribute("Duration", duration);

    if(pulse)
        buzzerXmlElement.setAttribute("Pulse", "True");
    else
        buzzerXmlElement.setAttribute("Pulse", "False");

    //return by value
    return buzzerXmlElement;
}
